﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Stock.物品信息
{
    public partial class GoodsAddAndEdit : Form
    {
        //用于窗体间传递值
        public int Id;
        public string GName;
        public string UnitePrice;
        public string Manufacture;
        public string Category;
        public Goods f;//桥梁
        public GoodsAddAndEdit()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //非空判断
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("带*为必填项");
            }
            else
            {
                string Id = textBox1.Text;
                string GName = textBox2.Text;
                string UnitePrice = textBox3.Text;
                string Manufacture = textBox4.Text;
                string Category = comboBox1.Text;
                string sql = $"insert into Goods values('{Id}','{GName}','{UnitePrice}','{Manufacture}','{Category}')";
                if (DBH.NoSelect(sql) > 0)
                {
                    MessageBox.Show("新增成功");
                    f.dataGridView1.AutoGenerateColumns = false;
                    f.dataGridView1.DataSource = DBH.Select("select * from Goods");
                }
                else
                {
                    MessageBox.Show("新增失败");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "")
            {
                MessageBox.Show("带*为必填项");
            }
            else
            {
                //获取最新值
                string Id = textBox1.Text;
                string GName = textBox2.Text;
                string UnitePrice = textBox3.Text;
                string Manufacture = textBox4.Text;
                string Category = comboBox1.Text;


                string sql = $"update Goods set GName='{1}',UnitePrice='{2}',Manufacture ='{3}',Category='{4}' where Id={Id}";
                if (DBH.NoSelect(sql) > 0)
                {
                    MessageBox.Show("保存成功！");
                    f.dataGridView1.AutoGenerateColumns = false;
                    f.dataGridView1.DataSource = DBH.Select("select * from Goods");
                }
                else
                {
                    MessageBox.Show("修改失败！");
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void GoodsAddAndEdit_Load(object sender, EventArgs e)
        {
            //初始化
            string Id = textBox1.Text;
            string GName = textBox2.Text;
            string UnitePrice = textBox3.Text;
            string Manufacture = textBox4.Text;
            string Category = comboBox1.Text;
        }
    }
}
